﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

public partial class EditGalery_foto : System.Web.UI.Page
{
    internal class CustomCompare : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            return DateTime.Compare(
              Directory.GetCreationTime(y),Directory.GetCreationTime(x));
        }
    }

    string[] dirrArray2 = new string[1];
    string[] FOlderList = new string[1];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated == false)
        { Server.Transfer("login_aizliegts.aspx"); }
        else
        {
            string virtualPathDir2 = Server.MapPath("~/");
            DataTable Mape = new DataTable("Mape");
            Mape.ReadXml(virtualPathDir2 + "Mape.xml");
            Array.Resize(ref FOlderList, Mape.Rows.Count);
            for (int i = 1; i < Mape.Rows.Count; i++)
            {
                FOlderList[i] = Mape.Rows[i][1].ToString().Substring(10);
            }
            Repeater1.DataSource = FOlderList;
            Repeater1.DataBind();
        }
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int MapesNr = e.Item.ItemIndex;
        string virtualPathDir2 = Server.MapPath("~/");
        DataTable Mape = new DataTable("Mape");
        Mape.ReadXml(virtualPathDir2 + "Mape.xml");

        int Nr = (int)Mape.Rows[MapesNr][0];
        Response.Redirect("EditGalery.aspx?value=" + Nr);
    }

    void AddNewMap_and_IMG()
    {
        string virtualPathDir2 = Server.MapPath("~/");
        DataTable galery = new DataTable("galery");
        galery.ReadXml(virtualPathDir2 + "galery.xml");
        DataTable Mape = new DataTable("Mape");
        Mape.ReadXml(virtualPathDir2 + "Mape.xml");

        string virtualPathDir = Server.MapPath("~/galerijas/"); // atrod virtual path lapas Bāzes MAPI
        string[] dirrAray = Directory.GetDirectories(virtualPathDir); // nolasa visas servera Mapes
        Array.Sort(dirrAray, new CustomCompare()); // Sortēšana
        string[] dirrArray2 = new string[dirrAray.Length];
        int cipars = 0; // Sagrupē mapes tā lai jaunākā ir pēdējā !!
        for (int i = dirrAray.Length; i > 0; i--)
        {
            dirrArray2[cipars] = dirrAray[i - 1];
            cipars++;
        }
        string virtualPathDir3 = Server.MapPath("~/");
        int charCount = virtualPathDir3.LastIndexOf("\\") + 1;
        bool saglabā = false;
        for (int ii = 0; ii < dirrArray2.Length; ii++) // iziet cauri visām Foto mapēm.
        {
            // Meklē neievadīto Mapi, ja atrod tad ievada.
            bool Nav = true;
            foreach (DataRow Map in Mape.Rows)
            {
                if (dirrArray2[ii].Substring(charCount) == (string)Map[1])
                {
                    Nav = false; // Ja atrod tad neko nevada.
                    
                }
            }
            if (Nav) // ja mapes nav ta taisa jaunu.
            {   
                saglabā = true;
                DataRow NewR = Mape.NewRow();
                int MapID = (int)Mape.Rows[Mape.Rows.Count - 1][0] + 1;
                NewR[0] = MapID;
                NewR[1] = dirrArray2[ii].Substring(charCount); ; // mapes nosaukums.
                NewR[2] = Directory.GetCreationTime(dirrArray2[ii]);
                NewR[3] = " ";
                Mape.LoadDataRow(NewR.ItemArray, true);
                //***  Ieraksta Foto failā datus.
                string[] fotoFile = Directory.GetFiles(dirrArray2[ii]);
                int PicturCount = 0;
                //btn_count = fotoFile.Length;
                //string textID = "Lbutton";
                for (int i = 0; i < fotoFile.Length; i++)
                {

                    string EsošaisTeksts = fotoFile[i].Substring(charCount);
                    string[] TextGabali = EsošaisTeksts.Split('\\');
                    bool smallimg = false;
                    smallimg = TextGabali[2].Contains("Small");
                    if (smallimg == false)
                    {    // vispirms atrod Lielo bildi un tad meklē mazo, ja tāda ir, ja nav ta mazo neliek. 
                        DataRow NewB = galery.NewRow(); // "ID","MapID", "NAME", "DATE", "IMG","IMGsmall", "TEXT"
                        NewB[0] = (int)galery.Rows[galery.Rows.Count - 1][0] + 1;
                        NewB[1] = MapID;
                        NewB[2] = TextGabali[2];
                        NewB[3] = File.GetCreationTime(fotoFile[i]);
                        string jaunaisLinks = "";
                        for (int i2 = 0; i2 < TextGabali.Length; i2++)
                        {
                            if (i2 == TextGabali.Length - 1)
                            {
                                jaunaisLinks += TextGabali[i2];
                            }
                            else { jaunaisLinks += TextGabali[i2] + "/"; }
                        }
                        NewB[4] = "http://www.parbmx.com/" + jaunaisLinks; // TextGabali[2]; // uztaisa IMG linku. ******
                        NewB[5] = " "; // Empty
                        NewB[6] = " "; // Empty

                        for (int aa = 0; aa < fotoFile.Length; aa++)
                        {
                            // serch for small img. 
                            string EsošaisTekstsX = fotoFile[aa].Substring(charCount);
                            string[] TextGabaliX = EsošaisTekstsX.Split('\\');
                            bool smallimgX = false;
                            smallimgX = TextGabaliX[2].Contains("Small");
                            if (smallimgX == true)
                            {
                                if (TextGabaliX[2].Remove(0, 5) == TextGabali[2]) // *********
                                {
                                    string jaunaisLinks2 = "";
                                    for (int i2 = 0; i2 < TextGabaliX.Length; i2++)
                                    {
                                        if (i2 == TextGabaliX.Length - 1)
                                        {
                                            jaunaisLinks2 += TextGabaliX[i2];
                                        }
                                        else { jaunaisLinks2 += TextGabaliX[i2] + "/"; }
                                    }
                                    NewB[5] = "http://www.parbmx.com/" + jaunaisLinks2; // fotoFile[aa]; // uzģenerē "Small" img linku.
                                    break;
                                }
                            }
                        }
                        galery.LoadDataRow(NewB.ItemArray, true);
                        // beigas  if (smallimg != true)
                    }
                }

            }

        }
        if (saglabā) // ja ir ko saglabāt tad saglabā izmaiņas XML failos.
        {
            DataSet ds = new DataSet(Mape.TableName + "Ds");
            ds.Tables.Add(Mape);
            ds.WriteXml(virtualPathDir2 + "Mape.xml", XmlWriteMode.WriteSchema);
            ds.Dispose();
            DataSet dss = new DataSet(galery.TableName + "Ds");
            dss.Tables.Add(galery);
            dss.WriteXml(virtualPathDir2 + "galery.xml", XmlWriteMode.WriteSchema);
            dss.Dispose();
        }

    }

    protected void Button1AddIMG_Click(object sender, EventArgs e)
    {
        AddNewMap_and_IMG();
    }

    protected void GaleryXMLupdate_Click(object sender, EventArgs e)
    {
        FillGaleryXMLfiles();  
    }
    void FillGaleryXMLfiles()
    {
        string virtualPathDir2 = Server.MapPath("~/");
        DataTable galery = new DataTable("galery");
        galery.ReadXml(virtualPathDir2 + "galery.xml");
        DataTable Mape = new DataTable("Mape");
        Mape.ReadXml(virtualPathDir2 + "Mape.xml");

        string virtualPathDir = Server.MapPath("~/galerijas/"); // atrod virtual path lapas Bāzes MAPI
        string[] dirrAray = Directory.GetDirectories(virtualPathDir); // nolasa visas servera Mapes
        Array.Sort(dirrAray, new CustomCompare()); // Sortēšana
        string[] dirrArray2 = new string[dirrAray.Length];
        int cipars = 0; // Sagrupē mapes tā lai jaunākā ir pēdējā !!
        for (int i = dirrAray.Length; i > 0; i--)
        {
            dirrArray2[cipars] = dirrAray[i - 1];
            cipars++;
        }
        string virtualPathDir3 = Server.MapPath("~/");
        int charCount = virtualPathDir3.LastIndexOf("\\") + 1;

        for (int ii = 0; ii < dirrArray2.Length; ii++) // iziet cauri visām Foto mapēm.
        {
            DataRow NewR = Mape.NewRow();
            int MapID = (int)Mape.Rows[Mape.Rows.Count - 1][0] + 1;
            NewR[0] = MapID;
            NewR[1] = dirrArray2[ii].Substring(charCount); ; // mapes nosaukums.
            NewR[2] = Directory.GetCreationTime(dirrArray2[ii]);
            NewR[3] = " ";
            Mape.LoadDataRow(NewR.ItemArray, true);

            string[] fotoFile = Directory.GetFiles(dirrArray2[ii]);
            //string virtualPathDir3 = Server.MapPath("~/");
            //int charCount = virtualPathDir3.LastIndexOf("\\") + 1;

            int PicturCount = 0;
            //btn_count = fotoFile.Length;
            //string textID = "Lbutton";
            for (int i = 0; i < fotoFile.Length; i++)
            {


                string EsošaisTeksts = fotoFile[i].Substring(charCount);
                string[] TextGabali = EsošaisTeksts.Split('\\');
                bool smallimg = false;
                smallimg = TextGabali[2].Contains("Small");
                if (smallimg == false)
                {    // vispirms atrod Lielo bildi un tad meklē mazo, ja tāda ir, ja nav ta mazo neliek. 
                    DataRow NewB = galery.NewRow(); // "ID","MapID", "NAME", "DATE", "IMG","IMGsmall", "TEXT"
                    NewB[0] = (int)galery.Rows[galery.Rows.Count - 1][0] + 1;
                    NewB[1] = MapID;
                    NewB[2] = TextGabali[2];
                    NewB[3] = File.GetCreationTime(fotoFile[i]);
                    string jaunaisLinks = "";
                    for (int i2 = 0; i2 < TextGabali.Length; i2++)
                    {
                        if (i2 == TextGabali.Length - 1)
                        {
                            jaunaisLinks += TextGabali[i2];
                        }
                        else { jaunaisLinks += TextGabali[i2] + "/"; }
                    }
                    NewB[4] = "http://www.parbmx.com/" + jaunaisLinks; // TextGabali[2]; // uztaisa IMG linku. ******
                    NewB[5] = " "; // Empty
                    NewB[6] = " "; // Empty

                    for (int aa = 0; aa < fotoFile.Length; aa++)
                    {
                        // serch for small img. 
                        string EsošaisTekstsX = fotoFile[aa].Substring(charCount);
                        string[] TextGabaliX = EsošaisTekstsX.Split('\\');
                        bool smallimgX = false;
                        smallimgX = TextGabaliX[2].Contains("Small");
                        if (smallimgX == true)
                        {
                            if (TextGabaliX[2].Remove(0, 5) == TextGabali[2]) // *********
                            {
                                string jaunaisLinks2 = "";
                                for (int i2 = 0; i2 < TextGabaliX.Length; i2++)
                                {
                                    if (i2 == TextGabaliX.Length - 1)
                                    {
                                        jaunaisLinks2 += TextGabaliX[i2];
                                    }
                                    else { jaunaisLinks2 += TextGabaliX[i2] + "/"; }
                                }
                                NewB[5] = "http://www.parbmx.com/" + jaunaisLinks2; // fotoFile[aa]; // uzģenerē "Small" img linku.
                                break;
                            }
                        }
                    }
                    galery.LoadDataRow(NewB.ItemArray, true);
                    // beigas  if (smallimg != true)
                }

            }
        }

        DataSet ds = new DataSet(Mape.TableName + "Ds");
        ds.Tables.Add(Mape);
        ds.WriteXml(virtualPathDir2 + "Mape.xml", XmlWriteMode.WriteSchema);
        ds.Dispose();
        DataSet dss = new DataSet(galery.TableName + "Ds");
        dss.Tables.Add(galery);
        dss.WriteXml(virtualPathDir2 + "galery.xml", XmlWriteMode.WriteSchema);
        dss.Dispose();
    }
}
